<?php
require_once dirname(__FILE__) . '/../config/config.php';
class GameMessageController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameMessService = new GameMessageService();
        $gameCategoryService = new GameCategoryService();       
        $pager = new Pager();
        
        $curpage = isset ($_GET['page']) ? $_GET['page'] : 1;
    	if(isset($_GET['database']) && !empty($_GET['database']))
        {
        	$data = $_GET['database'];
        	if($data == 'tpg')
        	{	
        		$gameCategoryArr = $gameCategoryService -> getFullGameCategoryTPG($context, $result);
        		if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
		        		$gameMessArr = $gameMessService -> getAllGameMessByGameCateIDTPG($context, $result, $filter);
		        		$lastStr = "controller=gameMessage&action=list&database=tpg&filter=filterGame";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
	        	else 
	        	{
	        		$count = $gameMessService->countGameMessTPG($context, $result);
		        	$limit = 50;
		        	$start = $pager->findStart($limit);
		        	$pages = $pager->findPages($count, $limit);
		        	$gameMessArr = $gameMessService -> getAllGameMessByLimitTPG($context, $result, $start, $limit);
		        	$lastStr = "controller=gameMessage&action=list&database=tpg";
	        	}
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        	}
	        else if($data == 'cgmb')
	        {
	        	$gameCategoryArr = $gameCategoryService -> getFullGameCategoryCGMB($context, $result);
	        	if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
		        		$gameMessArr = $gameMessService -> getAllGameMessByGameCateIDCGMB($context, $result, $filter);
		        		$lastStr = "controller=gameMessage&action=list&database=cgmb&filter=filterGame";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
	        	else 
	        	{
		        	$count = $gameMessService->countGameMessCGMB($context, $result);
		        	$limit = 30;
		        	$start = $pager->findStart($limit);
		        	$pages = $pager->findPages($count, $limit);
		        	$gameMessArr = $gameMessService->getAllGameMessByLimitCGMB($context, $result, $start, $limit);
		        	$lastStr = "controller=gameMessage&action=list&database=cgmb";
	        	}
	        	$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
	        }
	        else if($data == 'nsgt')
	        {
	        	$gameCategoryArr = $gameCategoryService -> getFullGameCategoryNSGT($context, $result);
	        	if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
		        		$gameMessArr = $gameMessService -> getAllGameMessByGameCateIDNSGT($context, $result, $filter);
		        		$lastStr = "controller=gameMessage&action=list&database=nsgt&filter=filterGame";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
	        	else 
	        	{
		        	$count = $gameMessService -> countGameMessNSGT($context, $result);
		        	$limit = 30;
		        	$start = $pager->findStart($limit);
		        	$pages = $pager->findPages($count, $limit);
		        	$gameMessArr = $gameMessService->getAllGameMessByLimitNSGT($context, $result, $start, $limit);
		        	$lastStr = "controller=gameMessage&action=list&database=nsgt";
	        	}
	        	$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
	        }
	        else if($data == 'mmbg')
	        {
	        	$gameCategoryArr = $gameCategoryService -> getFullGameCategoryMMBG($context, $result);
	        	if(isset($_POST) && !empty($_POST))
	        	{
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filterGame')
	        		{
		        		$gameMessArr = $gameMessService -> getAllGameMessByGameCateIDMMBG($context, $result, $filter);
		        		$lastStr = "controller=gameMessage&action=list&database=mmbg&filter=filterGame";
	        		}
	        		$this->smarty->assign('filter', $filter);
	        	}
	        	else 
	        	{
		        	$count = $gameMessService -> countGameMessMMBG($context, $result);
		        	$limit = 30;
		        	$start = $pager -> findStart($limit);
		        	$pages = $pager -> findPages($count, $limit);
		        	$gameMessArr = $gameMessService -> getAllGameMessByLimitMMBG($context, $result, $start, $limit);
		        	$lastStr = "controller=gameMessage&action=list&database=mmbg";
	        	}
	        	$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
        	}
        	$this->smarty->assign('data', $data);
       	}
       	else 
       	{
       		$gameCategoryArr = $gameCategoryService -> getFullGameCategoryTPG($context, $result);
       		$count = $gameMessService -> countGameMessTPG($context, $result);
        	$limit = 30;
        	$start = $pager->findStart($limit);
        	$pages = $pager->findPages($count, $limit);
        	$gameMessArr = $gameMessService->getAllGameMessByLimitTPG($context, $result, $start, $limit);        	
       		$lastStr = "controller=gameMessage&action=list&database=tpg";
       		$this->smarty->assign('gameCategoryArr', $gameCategoryArr);
       	}
       	if(isset($pager) && isset($pages) && isset($lastStr))
        {
	        $pageFirst = $pager->findFirst($curpage, $pages, $lastStr);
	        $pagePrev = $pager->findPrev($curpage, $pages, $lastStr);
	        $pageList = $pager->pageList($curpage, $pages, $lastStr);
	        $pageNext = $pager->findNext($curpage, $pages, $lastStr);
	        $pageLast = $pager->findLast($curpage, $pages, $lastStr);
	
	        $this->smarty->assign('pageFirst', $pageFirst);
	        $this->smarty->assign('pagePrev', $pagePrev);
	        $this->smarty->assign('pageList', $pageList);
	        $this->smarty->assign('pageNext', $pageNext);
	        $this->smarty->assign('pageLast', $pageLast);
        }
        if(isset($gameMessArr))
        {
        	$this->smarty->assign('gameMessArr', $gameMessArr);
        }
        $this->smarty->display('list-game-message.tpl');
    }

    public function addAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameCategoryService  = new GameCategoryService();
        $gameMessageService = new GameMessageService();
        $gameCategoryArr = $gameCategoryService->getFullGameCategory($context, $result);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $gameMessageService -> insertGameMessage($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $this->smarty->display('add-game-message.tpl');
        }
    }
    
	public function addMMBGAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameCategoryService  = new GameCategoryService();
        $gameMessageService = new GameMessageService();
        $gameCategoryArr = $gameCategoryService->getFullGameCategoryMMBG($context, $result);
		
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $gameMessageService -> insertGameMessageMMBG($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=mmbg");
        }
        else
        {
            $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
            $this->smarty->display('add-game-message-mmbg.tpl');
        }
    }

    public function updateAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        
        $gameCategoryService  = new GameCategoryService();  
        $gameMessageService = new GameMessageService();    
        
        if(isset($_GET['database']) && !empty($_GET['database']))
		{
			$data = $_GET['database'];
			if($data == 'tpg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
        		$gameCategoryArr = $gameCategoryService->getFullGameCategory($context, $result);
				$gameMessage = $gameMessageService -> getGameMessByIDTPG($context, $result, $id);
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
		        $this->smarty->assign('gameMessage', $gameMessage);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameMessageService->updateGameMessageTPG($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=tpg");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-message.tpl');
			}
			elseif ($data == 'cgmb')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
        		$gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);
				$gameMessage = $gameMessageService -> getGameMessByIDCGMB($context, $result, $id);
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
		        $this->smarty->assign('gameMessage', $gameMessage);
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameMessageService->updateGameMessageCGMB($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=cgmb");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-message.tpl');
			}
			elseif ($data == 'mmbg')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);
				$gameMessage = $gameMessageService -> getGameMessByIDMMBG($context, $result, $id);
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
		        $this->smarty->assign('gameMessage', $gameMessage);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameMessageService->updateGameMessageMMBG($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=mmbg");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-message.tpl');
			}
			elseif ($data == 'nsgt')
			{
				if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
		        $gameCategoryArr = $gameCategoryService->getFullGameCategoryCGMB($context, $result);
				$gameMessage = $gameMessageService -> getGameMessByIDNSGT($context, $result, $id);
		        $this->smarty->assign('gameCategoryArr', $gameCategoryArr);
		        $this->smarty->assign('gameMessage', $gameMessage);
		        
		        if(isset($_POST) && !empty($_POST))
		        {
		            $_POST['id'] = $_POST['id'];
		            $param = $_POST;
		            $gameMessageService->updateGameMessageNSGT($context, $result, $param);
		
		            header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=nsgt");
		        }
		        $this->smarty->assign('data',$data);
		     	$this->smarty->display('update-game-message.tpl');
			}
			
			$this->smarty->assign('data',$data);
		}

    }

    public function deleteAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $gameMessageService = new GameMessageService();
    	if(isset($_GET['database']) && !empty($_GET['database']))
		{
			$data = $_GET['database'];
			if($data == 'tpg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameMessageService->deleteGameMessageTPG($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=tpg");
			}
			
			if($data == 'cgmb')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameMessageService->deleteGameMessageCGMB($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=cgmb");
			}
			if($data == 'mmbg')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameMessageService->deleteGameMessageMMBG($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=mmbg");
			}
			if($data == 'nsgt')
			{
		        if(isset($_GET['id']) && !empty($_GET['id']))
		        {
		            $id = $_GET['id'];
		        }
       
		        $gameMessageService->deleteGameMessageNSGT($context, $result, $id);
		        header("location:".WEB_BASE_URL."/index.php?controller=gameMessage&action=list&database=nsgt");
			}
			
		}
    }
    
	
}

?>